import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { Http } from '@angular/http';
import { InitService } from '../../../public/serve/init/init.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { DatePipe } from '@angular/common';
import { Service } from 'app/server/server';

declare var $: any;

@Component({
  selector: 'app-keyword-table-download',
  templateUrl: './keyword-table-download.component.html'
})
export class KeywordTableDownloadComponent implements OnInit {

  downloadThemeTableType: any = 'xml';
  loading: boolean;

  constructor(
    private server: Service,
    public initService: InitService,
    public removeTableService: RemoveTableService,
    private hintsService: HintsService,
    private request: CudTableService,
    private datePipe: DatePipe,
    private http: Http,
  ) { }

  ngOnInit() {
  }

  /**
   * 下载主题词表
   * TODO::未完成
   */
  _download(info: any) {
    this.loading = true;
    let url = this.server.baseUrl + 'Service/ThemeWords/ExportThemeTable';
    info.token = 'b25dbd7138cf48e09941087e6d9a2e93';
    this.request
      .postRequest(url, info)
      .then(res => {
        if (res.result_code === "0000") {
          if(info.exportType == 'xml'){
            this.server.openWindowXML(res.httpPath);
          }else{
            this.server.openWindowB(res.httpPath);
          }
        }else{
          this.hintsService.messages('下载失败！', res.result_code);
        }
        this.loading = false;
        this._init();
        return;
      })
      .catch(err => {
        this.hintsService.messages("error", "error");
        this._init();
        return;
      });
  }

  /**
    * 初始化
    */
  private _init() {
    this.initService._init();
    $("#keywordTableDownload").modal("hide");
  }

}
